/******************************************************************************
	Cobalt - utility libraries for building XNA games
	Copyright (C) 2008 Philip Djonov

	This program is free software; you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the Free
	Software Foundation; either version 2 of the License, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
	for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the
	
		Free Software Foundation, Inc.
		51 Franklin Street, Fifth Floor
		Boston, MA  02110-1301, USA.
******************************************************************************/

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Cobalt.Framework.Graphics.Content
{

	internal sealed class FontReader : ContentTypeReader<Font>
	{
		protected override Font Read( ContentReader input, Font existingInstance )
		{
			Font ret = new Font();

			ret.Characters = input.ReadRawObject<int[]>();
			ret.Sheets = input.ReadRawObject<Texture2D[]>();

			ret.Points = new Font.FontPoint[input.ReadInt32()];
			for( int i = 0; i < ret.Points.Length; i++ )
			{
				Font.FontPoint pt = new Font.FontPoint();

				pt.PointSize = input.ReadSingle();
				pt.LineHeight = input.ReadSingle();
				pt.BaseLine = input.ReadSingle();

				pt.Glyphs = new Font.Glyph[ret.Characters.Length];
				for( int j = 0; j < pt.Glyphs.Length; j++ )
				{
					int sheetIdx = input.ReadInt32();
					pt.Glyphs[j].Sheet = sheetIdx < 0 ? null : ret.Sheets[sheetIdx];

					pt.Glyphs[j].Width = input.ReadSingle();
					pt.Glyphs[j].Height = input.ReadSingle();

					pt.Glyphs[j].DrawOffset.X = input.ReadSingle();
					pt.Glyphs[j].DrawOffset.Y = input.ReadSingle();
					pt.Glyphs[j].DrawSize.X = input.ReadSingle();
					pt.Glyphs[j].DrawSize.Y = input.ReadSingle();

					pt.Glyphs[j].SourceRectangle.X = input.ReadInt32();
					pt.Glyphs[j].SourceRectangle.Y = input.ReadInt32();
					pt.Glyphs[j].SourceRectangle.Width = input.ReadInt32();
					pt.Glyphs[j].SourceRectangle.Height = input.ReadInt32();

					pt.Glyphs[j].KernA = input.ReadSingle();
					pt.Glyphs[j].KernB = input.ReadSingle();
					pt.Glyphs[j].KernC = input.ReadSingle();

					pt.Glyphs[j].OtherCaseIndex = input.ReadInt32();
				}

				ret.Points[i] = pt;
			}

			return ret;
		}
	}

}